package com.sparrow.support;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sparrow.constant.*;
import com.sparrow.core.Cache;
import com.sparrow.mvc.ActionEntity;

/**
 * Action支持类
 * 
 * @author zhanglizhi
 * @date Apr 9, 2011 10:33:05 PM
 * @version 1.0
 */
public class ActionSupport extends ActionFilterSupport {

	/**
	 * 接收到的request对象
	 */
	private ThreadLocal<HttpServletRequest> request=new ThreadLocal<HttpServletRequest>();
	/**
	 * 接收到的respose对象
	 */
	private ThreadLocal<HttpServletResponse> response=new ThreadLocal<HttpServletResponse>();

	public HttpServletRequest getRequest() {
		return request.get();
	}

	public void setRequest(HttpServletRequest request) {
		this.request.set(request);
	}

	public HttpServletResponse getResponse() {
		return response.get();
	}

	public void setResponse(HttpServletResponse response) {
		this.response.set(response);
	}

	public String getActionKey() {
		return super.getActionKey(this.getRequest());
	}

	public void setDirectionURLParameter(String... paramters) {
		List<String> urlParameters = new ArrayList<String>();
		for (int i = 0; i < paramters.length; i++) {
			urlParameters.add(paramters[i]);
		}
		this.getRequest().setAttribute(this.URLPARAMETERS, urlParameters);
	}

	/**
	 * 前台js输出 为了避免include等输出多句js情况， 添加actionKey字段 action.do action.jsp
	 * 
	 * @author zhanglizhi
	 * @date Jun 21, 2011
	 * @version 1.0
	 * @param strScript
	 */
	public void setJavascript(String strScript) {
		Object oactionKey = this.getRequest().getAttribute(
				"javax.servlet.include.servlet_path");
		String actionKey = null;
		if (oactionKey != null) {
			actionKey = oactionKey.toString();
		} else {
			actionKey = this.getRequest().getServletPath();
		}
		oactionKey = ((ActionEntity) Cache.getInstance().get(
				CACHE_KEY.ACTION_ENTITY, actionKey)).getSourceUrl();
		if (oactionKey != null) {
			actionKey = oactionKey.toString();
		}
		this.getRequest().setAttribute(actionKey,
				"<script type=\"text/javascript\">" + strScript + "</script>");

	}

	public void RegisterRequest(Map<String, ?> map) {
		Iterator<String> it = map.keySet().iterator();
		String key = null;
		while (it.hasNext()) {
			key = it.next();
			this.getRequest().setAttribute(key, map.get(key));
		}
	}

	public <T> void putEntity(T entity) {
		this.getRequest().setAttribute(
				entity.getClass().getSimpleName().toLowerCase(), entity);
	}

	public String getCurrentUserId() {
		return super.getCurrentUserId(this.getRequest());
	}

	public String getCurrentUserName() {
		return super.getCurrentUserName(this.getRequest());
	}

	public String getBasePath() {
		return super.getBasePath(this.getRequest());
	}

	public void setError(String errorCtrlId, String error) {
		this.getRequest().setAttribute(errorCtrlId + ".innerHTML", error);
		this.getRequest().setAttribute(errorCtrlId + ".cssClass",
				COMMON.ERROR_CSS_CLASS);
	}

	public void initGridView(String gridView, ResultSet rs,
			String currentPageIndex, String pageSize, int totalRecordCount) {
		if (currentPageIndex == null) {
			currentPageIndex = "1";
		}
		this.getRequest().setAttribute(gridView + ".dataSource", rs);
		this.getRequest().setAttribute(gridView + ".recordCount",
				totalRecordCount);
		this.getRequest().setAttribute("spanRecordCount.innerHTML",
				totalRecordCount);
		this.getRequest().setAttribute(gridView + ".pageSize", pageSize);
	}

	public String getClientIp() {
		return super.getClientIp(this.getRequest());
	}

	public void clearClientCache() {
		this.getResponse().setHeader("Pragma", "No-cache");
		this.getResponse().setHeader("Cache-Control", "no-cache");
		this.getResponse().setDateHeader("Expires", 0);
	}
}
